<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请人" prop="applyName">
        <el-input
          v-model="queryParams.applyName"
          placeholder="请输入申请名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="applyRemark">
        <el-input
          v-model="queryParams.applyRemark"
          placeholder="请输入备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['approval:apply:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['approval:apply:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['approval:apply:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['approval:apply:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类型" align="center" prop="basicinfo.flowName" />
      <el-table-column label="申请人" align="center" prop="applyName" />
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="applyRemark" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <!--0：未审核，1：同意，2：驳回，-1：拒绝，3：审核中，4：完成，5：删除-->
          <span v-if="scope.row.status==='0'">未审核</span>
          <span v-if="scope.row.status==='1'">同意</span>
          <span v-if="scope.row.status==='-1'">拒绝</span>
          <span v-if="scope.row.status==='3'">审核中</span>
          <span v-if="scope.row.status==='4'">完成</span>
          <span v-if="scope.row.status==='5'">删除</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            v-if="scope.row.status==='3'||scope.row.status==='-1'"
            size="mini"
            type="text"
            @click="selectCourse(scope.row)"
          >进程</el-button>

          <el-button
            v-if="scope.row.status==='0'"
            size="mini"
            type="text"
            @click="applicant(scope.row)"
            v-hasPermi="['approval:apply:edit']"
            v-loading.fullscreen.lock="fullscreenLoading"
          >申请</el-button>

          <el-button
            v-if="scope.row.status==='0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['approval:apply:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.status!=='3'||scope.row.status==='0'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['approval:apply:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请类型" prop="flowId">
          <el-select v-model="form.flowId" placeholder="请选择">
            <el-option
              v-for="item in basicinfoList"
              :key="item.id"
              :label="item.flowName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="applyRemark">
          <el-input
            type="textarea"
            :rows="2"
            placeholder="请输入内容"
            v-model="form.applyRemark">
          </el-input>
        </el-form-item>
        <el-form-item label="类型" prop="flowCaiGouApply">
          <el-checkbox-group v-model="flowCaiGouApply.arrCaigou">
            <el-checkbox label="1">镀锌基建</el-checkbox>
            <el-checkbox label="2">镀锌生产线</el-checkbox>
            <el-checkbox label="3">办公用品</el-checkbox>
            <el-checkbox label="0">其他</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--process 黑  finish 蓝 success 绿 wait 白 error  红-->
    <el-dialog title="进程" :visible.sync="openCourse" width="50%"  append-to-body >
      <el-steps :active="nodeId" align-center  finish-status="success" :process-status="processStyle">
        <el-step title="开始" ></el-step>
        <el-step v-for ="item in flowList" :key="item.id" :title="item.nodeType==='1'?'审批人':'抄送人'"
                 description="description">
          <template slot="description" >
            <span>
              <div class="course-style">{{item.approverUid}}</div>
              <div class="course-style">{{parseTime(item.approvalTime, '{y}-{m}-{d} {h}:{i}')}}</div>
              <div class="course-style" v-if="item.approverRemark!=null">意见：{{item.approverRemark}}</div>
            </span>
          </template>
        </el-step>
      </el-steps>
    </el-dialog>
  </div>
</template>

<script>
import { listApply, getApply, delApply, addApply, updateApply, exportApply,startApply,getCourse } from "@/api/approval/apply";
import {listBasicinfo} from "@/api/approval/basicinfo"


export default {
  name: "Apply",
  components: {

  },
  data() {
    return {
      fullscreenLoading:false,
      //采购类型
      flowCaiGouApply:{
        arrCaigou:[]
      },
      //流程进程
      flowList:[],
      //进程样式
      processStyle:"",
      //当前审核节点
      nodeId:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //流程类型
      basicinfoList:[],
      // 申请表格数据
      applyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查看进程
      openCourse: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flowId: null,
        applyName: null,
        applyTime: null,
        applyRemark: null,
        status: null,
        detailId: null,
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 进度条*/
    selectCourse(row){
      getCourse(row.id).then(res => {
        console.log(res)
        this.openCourse = true;
        this.flowList = res.rows
        let then = this
        for (let i = 0; i<then.flowList.length;i++) {
          if (then.flowList[i].status === "1") {
            this.processStyle = "finish"
            if (then.flowList.length-i!==1) {
              /*节点不为最后*/
              this.nodeId = i+1
            } else if (then.flowList[i].status === "1"&&then.flowList.length-i===1) {
              /*最后一个节点*/
              this.nodeId = then.flowList.length
            }
          }else if (then.flowList[i].status === "-1"){
            this.processStyle = "error"
            this.nodeId = i+1
          }
        }
      })
    },

    /** 发起申请 */
    applicant(row){
      this.fullscreenLoading = true
      startApply(row).then(response =>{
        if (response.code===200){
          this.fullscreenLoading = false
          this.msgSuccess("申请成功");
          this.getList();
        }
      })
    },

    /** 查询申请列表 */
    getList() {
      this.loading = true;
      listApply(this.queryParams).then(response => {
        this.applyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listBasicinfo().then(response =>{
        this.basicinfoList = response.rows
      })

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        flowId: null,
        applyName: null,
        applyTime: null,
        applyRemark: null,
        status: "0",
        detailId: null
      };
      this.flowCaiGouApply.arrCaigou=[]
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApply(id).then(response => {
        this.form = response.data;
        this.flowCaiGouApply= this.form.flowCaiGouApply
        this.open = true;
        this.title = "修改申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.flowCaiGouApply = this.flowCaiGouApply
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApply(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApply(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除申请编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delApply(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有申请数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportApply(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
<style>
  .course-style{
    color: #000;
  }
</style>
